package api

import (
	"errors"
	string2 "longmen/server/pkg/common/helper/string"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/common/payment"
	"net/http"

	"github.com/labstack/echo/v4"
	"longmen/server/pkg/common/api_models"
)

// @Summary 支付帳號列表
// @Description 支付帳號列表
// @Accept  json
// @Produce  json
// @Tags Order
// @Success 200 {object} models_main.PaymentApi "result"
// @Router /api/order/getPaymentApiList [POST]
func (base *Controller) GetPaymentApiList(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	resp, err := base.Service.GetPaymentApiList(user)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

func (base *Controller) GetDepositList(c echo.Context) error {

	r := &api_models.GetDepositListReq{}
	_ = util.Json2Struct(c.Get("body").(string), r)
	r.Page = string2.GetDefaultNum(r.Page, 1)
	r.Size = string2.GetDefaultNum(r.Size, 10)

	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	resp, err := base.Service.GetDepositList(r, user)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 消費類型
// @Description 消費類型
// @Accept  json
// @Produce  json
// @Tags Order
// @Success 200 {object} api_models.GetSpendTypeListResp "result"
// @Router /api/order/getSpendTypeList [POST]
func (base *Controller) GetSpendTypeList(c echo.Context) error {

	spendTypes := make([]*api_models.SpendType, 0)
	spendTypes = append(spendTypes, &api_models.SpendType{Id: 1, Name: "礼物"})
	resp := &api_models.GetSpendTypeListResp{
		List: spendTypes,
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 消费明細 需登入
// @Description 消费明細 需登入
// @Accept  json
// @Produce  json
// @Tags Order
// @Param   sendBody body api_models.GetSpendListReq true "body"
// @Success 200 {object} api_models.GetIncomeListResp "result"
// @Router /api/order/getSpendList [POST]
func (base *Controller) GetSpendList(c echo.Context) error {

	r := &api_models.GetSpendListReq{}
	_ = util.Json2Struct(c.Get("body").(string), r)
	r.Page = string2.GetDefaultNum(r.Page, 1)
	r.Size = string2.GetDefaultNum(r.Size, 10)

	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	resp, err := base.Service.GetSpendList(r, user)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 收入明細 需登入
// @Description 收入明細 需登入
// @Accept  json
// @Produce  json
// @Tags Order
// @Param   sendBody body api_models.GetIncomeListReq true "body"
// @Success 200 {object} api_models.GetIncomeListResp "result"
// @Router /api/order/getIncomeList [POST]
func (base *Controller) GetIncomeList(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	//if user.IsAnchor <= 0 {
	//    return common.BuildFail(c, 1, "该账号尚未认证主播", nil)
	//}
	r := &api_models.GetIncomeListReq{}
	_ = util.Json2Struct(c.Get("body").(string), r)
	r.Page = string2.GetDefaultNum(r.Page, 1)
	r.Size = string2.GetDefaultNum(r.Size, 10)

	resp, err := base.Service.GetIncomeList(r, user)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

//@Summary 充值
//@Description 充值
//@Accept  json
//@Produce  json
//@Tags Order
//@Param   sendBody body api_models.SendOrderReq true "body"
//@Success 200 {object} api_models.Response "result"
//@Router /api/order/send [POST]
func (base *Controller) SendOrder(c echo.Context) error {
	r := &api_models.SendOrderReq{}
	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
		return util.BadParam(c, err)
	}
	host := "https://" + c.Request().Host
	ip := c.RealIP()
	uas := c.Request().Header["User-Agent"]
	isMobile := false
	if len(uas) > 0 {
		isMobile = util.IsMobile(uas[0])
	}
	lang := c.Request().Header.Get("x-lang")
	if len(lang) <= 0 {
		lang = "zh-CN"
	}
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	resp, err := base.Service.SendOrder(host, ip, isMobile, lang, r, user)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

func (base *Controller) Notify(c echo.Context) error {
	id := util.S2I(c.Param("id"))
	if id <= 0 {
		return util.BadParam(c, errors.New("id is nil"))
	}
	notify, err := base.Service.Notify(id, c)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	var ret error
	switch notify.Type {
	case payment.JSON:
		ret = c.JSON(http.StatusOK, notify.Ret)
	case payment.XML:
		ret = c.XML(http.StatusOK, notify.Ret)
	case payment.STRING:
		ret = c.String(http.StatusOK, notify.Ret.(string))
	case payment.HTTPSTATUSCODE:
		ret = c.String(notify.HttpStatusCode, notify.Ret.(string))
	default:
		ret = c.JSON(http.StatusOK, notify.Ret)
	}
	return ret
}
